<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;
use Vendor\Openwx\Openwx;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

	//系统首页
    public function index(){
    	echo '';
    }






    public function rediresurl()
    {
        $remark = I('get.remark');
        $url = U('authorInfo',array('remark'=>$remark));
        $str = "跳转中...<script language='javascript' type='text/javascript'>window.location.href='$url'</script>";  
        $this->show($str);
    }


    //授权并保存信息
    public function authorInfo()
    {

    	$auth_code = I('get.auth_code');
    	$remark = I('get.remark');
    	//判断是否有权限
    	$publicnumber = M('publicnumber');
    	$authinfo = $publicnumber->where(array('remark'=>$remark))->find();
    	if (!$authinfo) {
    		echo '暂无授权权限';exit;
    	}  
    	$openwx = new Openwx();  	
    	if (!$auth_code) {
    		//获取auth_code
    		$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
			$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
			$openwx->loginAuth($url);
    	} else {
    		//授权成功，保存公众号信息
    		$info = $openwx->authorInfo($auth_code);
    		$data = array();
    		if ($info) {
    			$data['authorizer_appid'] = $info['authorization_info']['authorizer_appid'];
    			$publicinfo = $openwx->wxAuthorInfo($info['authorization_info']['authorizer_appid']);
    			$data['nick_name'] = $publicinfo['authorizer_info']['nick_name'];
    			$data['head_img'] = $publicinfo['authorizer_info']['head_img'];
    			$data['user_name'] = $publicinfo['authorizer_info']['user_name'];
    			$data['principal_name'] = $publicinfo['authorizer_info']['principal_name'];
                $data['alias'] = $publicinfo['authorizer_info']['alias'];
    			$data['qrcode_url'] = 'http://'.$_SERVER[HTTP_HOST].'/'.downImg($publicinfo['authorizer_info']['qrcode_url'],'Uploads/Picture/');
    			$data['createtime'] = time();
    			$data['status'] = 1;
    			$rel = $publicnumber->where(array('remark'=>$remark))->save($data);
    			if ($rel) {
    				header('location:http://'.$authinfo['auth_url'].'/super.php?s=/System/systemset');exit;
    			}

    		}

    	}
    }



    //网页认证中间件
    public function webAuth()
    {
        $wxappid = I('get.appid');
        $nowurl = I('get.nowurl');
        $openwx = new Openwx();
        $code = I('get.code');
        if (!$code){
            $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
            $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";   
            $openwx->getOpenAuth($wxappid,$url,'snsapi_userinfo');
        }        
        $token = $openwx->getOpenAuthAccessToken($wxappid,$code);
        $userinfo = $openwx->getOpenAuthInfo($token['access_token'],$token['openid']);
        $openid = $token['openid'];   
        if ($openid) {
            $nowurl = str_replace('dxgeek', '/', $nowurl);
            $nowurl = base64_decode($nowurl);
            $url = $nowurl."&openid=".$openid."&access_token=".$token['access_token'];
            header('Location:'.$url);exit;
        }             
    }













    //api获取公众号信息
    public function getPublicnumberInfo()
    {
        $remark = I('post.remark');
        if (!$remark) {
            echo '参数错误';exit;
        }
        $publicnumber = M('publicnumber');
        $info = $publicnumber->where(array('remark'=>$remark))->find();
        echo json_encode($info);
    }




    //api接口获取gComponentAccessToken
    public function getComponentAccessToken()
    {
    	$openwx = new Openwx();
    	$getComponentAccessToken = $openwx->getComponentAccessToken();  
    	echo $getComponentAccessToken;
    }


    //api获取authorizer_access_token
    public function getAuthorizer_access_token()
    {
        $appid = I('post.appid');
        if (!$appid) {
            echo '参数错误';exit;
        }
        $openwx = new Openwx();
        $authorizer_access_token = $openwx->authorizer_access_token($appid);
        echo $authorizer_access_token;

    }


    


}